﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    public class CommentService : ICommentService
    {
        // 添加对商品的评论
        public long AddNew(CommentDTO comment)
        {
            Comment com = new Comment();
            com.CommentImgSrc = comment.CommentImgSrc;
            com.Content = comment.Content;
            com.GoodId = comment.GoodId;
            com.Level = comment.Level;
            com.OrderId = comment.OrderId;
            com.UserId = comment.UserId;

            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Comment> bs = new BaseService<Comment>(ctx);
                bool exist = bs.GetAll().Any(c => c.UserId == comment.UserId && c.GoodId == comment.GoodId && c.OrderId == comment.OrderId);
                if (exist)
                {
                    var data = bs.GetAll().Where(c => c.UserId == comment.UserId && c.GoodId == comment.GoodId && c.OrderId == comment.OrderId).SingleOrDefault();
                    throw new ArgumentException($"在订单{data.OrderId}中，{data.User}用户已经对{data.GoodId}商品进行了评价哦~~~");
                }
                ctx.Comments.Add(com);
                ctx.SaveChanges();
                return com.Id;

            }
        }

        public long GetTotalCountByUser(long userId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Comment> bs = new BaseService<Comment>(ctx);
                var count = bs.GetAll()
                    .AsNoTracking().Where(c => c.UserId == userId).Count();
                return count;
            }
        }

        // 获取该商品所有的评论

        public CommentDTO[] GetPageDataComment(long goodId, int index = 1)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Comment> bs = new BaseService<Comment>(ctx);
                var comments = bs.GetPageData(index, 10).AsNoTracking().Where(c => c.GoodId == goodId);
                if (comments == null)
                {
                    throw new ArgumentException($"商品id为{goodId}的商品还没有任何评论哦");
                }
                var res= comments.AsNoTracking()
                    .Include(e => e.User)
                    .Include(e => e.Good)
                    .ToList().Select(c => ToDTO(c)).ToArray();
                return comments.AsNoTracking()
                    .Include(e=>e.User)
                    .Include(e => e.Good)
                    .ToList().Select(c => ToDTO(c)).ToArray();
            }
        }

        // 获取该用户所有的评论
        public CommentDTO[] GetPageDataCommentByUserId(long userId, int pageIndex, int pageSize)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Comment> bs = new BaseService<Comment>(ctx);
                var comments = bs.GetAll().Include(c => c.User)
                    .Include(c => c.Good).Include(c => c.Order)
                    .AsNoTracking().Where(c => c.UserId == userId).OrderByDescending(o => o.CreateDateTime)
                        .Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return comments.ToList().Select(c => ToDTO(c)).ToArray();
            }
        }

        public void MakeDelete(long commentId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Comment> bs = new BaseService<Comment>(ctx);
                bs.MakeDelete(commentId);
            }
        }
        // 更新某个用户对某件商品的评论
        public void UpdateComment(CommentDTO comment)
        {

            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Comment> bs = new BaseService<Comment>(ctx);
                var com = bs.GetById(comment.Id);
                if (com == null)
                {
                    throw new ArgumentException("id=" + comment.UserId + "的该用户不存在");
                }


                com.CommentImgSrc = comment.CommentImgSrc;
                com.Content = comment.Content;
                com.CreateDateTime = comment.CreateDateTime;
                com.GoodId = comment.GoodId;
                com.Id = comment.Id;
                com.Level = comment.Level;
                com.OrderId = comment.OrderId;
                com.UserId = comment.UserId;


                ctx.SaveChanges();

            }
        }

        // 转化为DTO
        public CommentDTO ToDTO(Comment comment)
        {
            CommentDTO dto = new CommentDTO();
            dto.GoodName = comment.Good.Name;
            dto.UserName = comment.User.NiceName;
            dto.CommentImgSrc = comment.CommentImgSrc;
            dto.Content = comment.Content;
            dto.CreateDateTime = comment.CreateDateTime;
            dto.GoodId = comment.GoodId;
            dto.Id = comment.Id;
            dto.Level = comment.Level;
            dto.OrderId = comment.OrderId;
            dto.UserId = comment.UserId;
            dto.UserImgSrc = comment.User.ImgSrc;
            return dto;
        }

        // 获取所有的商品
        public CommentDTO[] GetAll()
        {

            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Comment> bs = new BaseService<Comment>(ctx);
                return bs.GetAll().AsNoTracking().Include(e=>e.User).ToList().Select(c => ToDTO(c)).ToArray();
            }
        }

        // 分页获取数据
        public CommentDTO[] GetPageData(int index = 1)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Comment> bs = new BaseService<Comment>(ctx);
                return bs.GetPageData(index, 10).ToList().Select(c => ToDTO(c)).ToArray();
            }
        }

        public CommentDTO GetById(long commentId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Comment> bs = new BaseService<Comment>(ctx);
                var data = bs.GetById(commentId);
                if (data == null)
                {
                    throw new ArgumentException($"评论id为{commentId}的评论不存在");
                }
                return ToDTO(data);
            }
        }

        // 获取该商品所有的评论数量

        public long GetDataCommentCountByGoodId(long goodId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Comment> bs = new BaseService<Comment>(ctx);
                return bs.GetAll().LongCount(e => e.GoodId == goodId);
            }
        }
    }
}
